<?php

namespace App\Kuafu\Banner\Controller\Applet;

use App\Kuafu\Banner\Enum\BannerCodeEnum;
use App\Kuafu\Banner\Enum\BannerEnum;
use App\Kuafu\Banner\Service\BannerCommonService;
use App\Kuafu\Banner\Service\BannerService;
use App\Kuafu\Banner\Validate\BannerValidate;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Service\ShopService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BannerController extends BaseController
{

    /**
     * 小程序端横幅列表-【根据横幅配置id查询横幅列表】
     *
     * @return JsonResponse
     */
    public function getBannerList(): JsonResponse
    {
        $brandId = $this->brandId ?: 1;
        $shopId  = $this->shopId;
        $banner  = BannerService::GetBannerList($brandId, $shopId);
        return $this->success($banner);
    }

    /**
     * 小程序端横幅详情
     *
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function getBannerById(Request $request, $id): JsonResponse
    {
        $id    = (int)$id;
        $valid = BannerValidate::getBannerById(['id' => $id]);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_NOT_INPUT, '非法数据，参数校验失败');
        }
        $info = BannerService::getBannerById($id);

        return $this->success($info);
    }

    /**
     * 获取小程序首页图片
     * @return JsonResponse
     */
    public function getAppletIndexImage(): JsonResponse
    {
        $type  = BannerEnum::TYPE_INDEX;
        $field = ['img', 'extraImg', 'name'];
        $data  = BannerCommonService::getAppletIndexImage($type, 0, 0, $field, 'first');
        return $this->success($data);
    }

    /**
     * 获取小程序弹窗
     * @return JsonResponse
     */
    public function getAppletPopImage(): JsonResponse
    {
        $type     = BannerEnum::TYPE_POP;
        $field    = ['img'];
        $shopId   = $this->shopId;
        $shopInfo = ShopService::getShopInfo($shopId);
        $data     = BannerCommonService::getAppletPopImage($type, $shopId, $shopInfo['type'], $field, 'first');
        return $this->success($data);
    }

}
